/*
 * DBManager.cpp
 *
 *  Created on: Aug 25, 2016
 *      Author: ubuntu
 */

#include "DBManager.h"
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>

DBManager::DBManager(const char *path)
{
	if (path == NULL)
		m_path = strdup("net/socket");
	else
		m_path = strdup(path);
	m_bTerminated = false;
	m_thread = 5;
//	m_count = 0;
	pthread_mutex_init(&data_mutex, NULL);
}

DBManager::~DBManager()
{
	pthread_mutex_destroy(&data_mutex);
	if (m_path)
		free(m_path);
}

void DBManager::stop()
{
//	debug(LOG_DEBUG, "消息总数: %u", m_count);
	m_bTerminated = true;
}

void DBManager::push(DataInfo *pDataInfo)
{
	pthread_mutex_lock(&data_mutex);
	m_lstDataInfo.push(pDataInfo);
	pthread_mutex_unlock(&data_mutex);
}

void *DBManager::DBThread(void *arg)
{
	return NULL;
}

void DBManager::run()
{
	while (!m_bTerminated) {
		DataInfo *pDataInfo = NULL;
		if (m_lstDataInfo.empty()) {
			usleep(1000);
			continue;
		}
		pthread_mutex_lock(&data_mutex);
		pDataInfo = m_lstDataInfo.front();
		m_lstDataInfo.pop();
		pthread_mutex_unlock(&data_mutex);
		if (pDataInfo)
		{
		//	m_count++;
			pDataInfo->writelog(m_path);
			delete pDataInfo;
		}
	}
}


